/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Text;
using System.IO;

namespace Madason.MetaModel
{
	[XmlRoot("model")]
	public class Model : Namespace
	{
		Hashtable namespaceMap = new Hashtable();
		Hashtable classMap = new Hashtable();

		public static Model LoadModel(string path)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(Model));
			FileStream fileStream = new FileStream(path, FileMode.Open);
			Model model = (Model)xmlSerializer.Deserialize(fileStream);
			return model;
		}

		public void SaveModel(string path)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(Model));
			FileStream fileStream = new FileStream(path, FileMode.Create);
			xmlSerializer.Serialize(fileStream, this);
		}


		/// <summary>
		/// will create a namespace structure
		/// 
		/// e.g.
		///   path = madason.testnamespace.subnamespace
		/// 
		/// will create:
		/// 
		/// madason
		/// +- testnamespace
		///    +- subnamespace
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		public Namespace CreateNamespace(string path)
		{
			Namespace parent = this;
			string[] parts = path.Split(".".ToCharArray());
			foreach (string part in parts)
			{
				Namespace ns = parent.FindByName(part);
				if (ns == null)
				{
					ns = new Namespace();
					ns.Name = part;
					ns.Parent = parent;
					parent.Namespaces.Add(ns);
				}
				parent = ns;
			}
			return parent;
		}

	}
}
